import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:get/get.dart';

// ignore: must_be_immutable
class WebViewPage extends StatelessWidget {
  WebViewPage({Key? key}) : super(key: key);
  final WebPageModel _webPageModel = Get.arguments;
  final GlobalKey webViewKey = GlobalKey();
  InAppWebViewSettings options = InAppWebViewSettings(
    useShouldOverrideUrlLoading: true,
    mediaPlaybackRequiresUserGesture: true,
    // android 支持HybridComposition
    useHybridComposition: true,
    //ios
    allowsInlineMediaPlayback: true,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(_webPageModel.title ?? '')),
        body: Builder(builder: (BuildContext context) {
          return InAppWebView(
            key: webViewKey,
            initialUrlRequest: URLRequest(url: WebUri(_webPageModel.url ?? '')),
            initialSettings: options,
            onScrollChanged: (inAppWebViewController, x, y) {},
          );
        }));
  }
}

class WebPageModel {
  String? title;
  String? url;

  WebPageModel({this.title, this.url});

  WebPageModel.fromJson(Map<String, dynamic> json) {
    if (json["title"] is String) title = json["title"];
    if (json["url"] is String) url = json["url"];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data["title"] = title;
    data["url"] = url;
    return data;
  }
}
